package main;

/*
 *
 * Copyright (c) 2009, Sun Microsystems, Inc.
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *  * Neither the name of Sun Microsystems nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
import ItineraryDays.*;
import com.sun.lwuit.Display;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.plaf.Style;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Resources;
import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.midlet.MIDlet;
import util.Menu;
import menus.*;
import util.Sound;

/**
 * Bootstraps the UI toolkit demos
 *
 * @author Shai Almog
 */
public class Main extends MIDlet implements ActionListener {

    private static final Menu[] menus = new Menu[]{
        new MainMenu(),new Camera(), new Currency(), new Itinerary(), new Landmarks(), new Map(),
        new Translator(), new Monday(), new Paris(), new Dublin()
    };
    private static Menu current_menu = menus[0];
    String prev_menu = "Exit";
    public void t() throws IOException, MediaException{

    InputStream stream = getClass().getResourceAsStream("/Audio/Jet.wav");
        try {
            Player p = Manager.createPlayer(stream, "audio/X-wav");
            p.realize();
            p.prefetch();
           
            p.start();
           
        }
        catch(IOException ioe){}
       catch(MediaException me){}
    }
    protected void startApp() {
        try {
            t();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (MediaException ex) {
            ex.printStackTrace();
        }
        Display.init(this);
        Hashtable themeProps = new Hashtable();
        themeProps.put("SoftButton.fgColor", "ffffff");
        themeProps.put("Title.fgColor", "ffffff");
        themeProps.put("SoftButton.bgColor", "0");
        themeProps.put("List.bgColor", "0");
        themeProps.put("fgSelectionColor", "ffffff");
        themeProps.put("Title.transparency", "0");
        // themeProps.put("SoftButton.transparency", "0");
        themeProps.put("bgSelectionColor", "0");
        themeProps.put("transparency", "0");
        themeProps.put("Button.transparency", "0");
        UIManager.getInstance().setThemeProps(themeProps);
        Style s = UIManager.getInstance().getComponentStyle("menu");
        UIManager.getInstance().setComponentStyle("menu", s);

        setMainForm();
    }

    /**
     * Used instead of using the Resources API to allow us to fetch locally downloaded
     * resources
     *
     * @param name the name of the resource
     * @return a resources object
     */
    public static Resources getResource(String name) throws IOException {
        return Resources.open("/" + name + ".res");
    }

    protected void pauseApp() {
    }

    protected void destroyApp(boolean arg0) {
    }

    void setMainForm() {

        menus[0].run(this);
    }

    public void actionPerformed(ActionEvent evt) {
        String Command = evt.getSource().toString();
        if (evt.getCommand() != null) {
            if (Command.equalsIgnoreCase("EXIT")) {
                this.notifyDestroyed();
            } else {
                if (Command.equalsIgnoreCase("Back")) {
                    changeMenu(prev_menu,false);
                } else {
                    changeMenu(Command,false);
                }
            }
        } else {
            String current = evt.getSource().toString();

            if (current.substring(0, 4).equalsIgnoreCase("List")) {
                //List actions - controlled by menu
            }
            if (current.substring(0, 6).equalsIgnoreCase("Button")) {
                //Button actions
                changeMenu(current_menu.Clicked,true);
            }
            if (current.substring(0, 5).equalsIgnoreCase("Label")) {
            }
        }
    }

    private boolean changeMenu(String Menu, boolean buildReaderIfFails) {
        Menu d = getMenu(Menu);

        if (d != null) {
            current_menu.cleanup();
            prev_menu = current_menu.getName();
            System.out.println("Opening...." + d.getName());
            d.run(this);
            current_menu = d;
            return true;
        } else {
            if (buildReaderIfFails) {
                new Reader(Menu).run(this);
            }

        }
        return false;
    }

    public Menu getMenu(String Demo_Name) {
        for (int i = 0; i < menus.length; i++) {
            if (menus[i].getName().equalsIgnoreCase(Demo_Name)) {
                return menus[i];
            }
        }

        return null;
    }
}
